package com.elong.businesstravel.modules.home.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.android.support.jhf.touchimageview.BasePagerAdapter;
import com.android.support.jhf.touchimageview.TouchImageView;
import com.android.support.jhf.touchimageview.GalleryViewPager;
import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.assist.FailReason;
import com.android.support.jhf.universalimageloader.core.assist.ViewScaleType;
import com.android.support.jhf.universalimageloader.core.imageaware.ImageAware;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.ImageUtil;
import com.elong.businesstravel.ELongBusinessTravelApplication;
import com.elong.businesstravel.R;
import com.elong.businesstravel.entitys.HotelImage;

public class HotelPictureAdapter extends BasePagerAdapter {

	private List<HotelImage> mImageList;
	private Context mContext;
	private ELongBusinessTravelApplication mApplication;
//	private Bitmap mDefaultBitmap = null;

	public HotelPictureAdapter(Context context, List<HotelImage> imageStrings) {
		super(context, null);
		mImageList = imageStrings;
		mContext = context;
		mApplication = (ELongBusinessTravelApplication) context
				.getApplicationContext();
		
//		mDefaultBitmap = ImageUtil.drawable2Bitmap(mContext.getResources().getDrawable(R.drawable.bg_guide));
	}

	@Override
	public int getCount() {
		return mImageList.size();

	}
	
	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {
		super.setPrimaryItem(container, position, object);
		((GalleryViewPager) container).mCurrentView = (TouchImageView) ((View) object)
				.findViewById(R.id.touchImageView);
	}

	@Override
	public Object instantiateItem(ViewGroup collection, int position) {
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.adapter_hotel_picture, null);

		final TouchImageView touchImageView = (TouchImageView) view
				.findViewById(R.id.touchImageView);
		final ViewGroup waitLayout = (ViewGroup) view
				.findViewById(R.id.waitLayout);
		waitLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
			}
		});
		waitLayout.setVisibility(View.VISIBLE);
		touchImageView.setLayoutParams(new RelativeLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
//		touchImageView.setImageBitmap(mDefaultBitmap);
		ImageLoader.getInstance().loadImage(mImageList.get(position).location,
				mApplication.mDisplayImageOptions,
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingStarted(String imageUri, View view) {
						waitLayout.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						touchImageView.setImageBitmap(loadedImage);
						waitLayout.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
					}
				});
		collection.addView(view, 0);
		return view;
	}

}
